<template>
  <div>
    <div style="margin-top: 5px; margin-bottom: 5px">
      <el-input
        v-model="name"
        placeholder="请输入仓库名"
        style="width: 200px"
        @keyup.enter="loadPost"
        ><template><el-icon class="el-input__icon"></el-icon></template
      ></el-input>
      <el-button type="primary" style="margin-left: 10px" @click="loadPost">查询</el-button>
      <el-button type="success" @click="resetParam">重置</el-button>
      <el-button type="primary" @click="add" style="margin-left: 10px">新增</el-button>
    </div>
    <el-table
      :data="tableData"
      style="height: 100; text-align: center"
      :header-cell-style="{ background: '#f2f5fc', color: '#555', textAlign: 'center' }"
      :cell-style="{ textAlign: 'center' }"
      border
      stripe
    >
      <el-table-column prop="id" label="ID" width="60" />
      <el-table-column prop="name" label="仓库名" width="100" />
      <el-table-column prop="remark" label="备注" />

      <el-table-column prop="operate" label="操作">
        <template v-slot="scope">
          <el-button type="success" @click="mod(scope.row)">编辑</el-button>
          <el-popconfirm
            confirm-button-text="确定"
            cancel-button-text="取消"
            title="确定要删除吗？"
            @confirm="del(scope.row.id)"
          >
            <template #reference>
              <el-button type="danger">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <div class="demo-pagination-block">
      <el-pagination
        v-model:current-page="pageNum"
        v-model:page-size="pageSize"
        :page-sizes="[5, 10, 20, 30]"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        style="text-align: center"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
      <el-dialog
        v-model="dialogVisible"
        title="添加仓库信息"
        width="30%"
        :before-close="handleClose"
        center
      >
        <el-form
          ref="form"
          :rules="rules"
          :model="form"
          label-width="80px"
          style="text-align: center; max-width: 500px"
        >
          <el-form-item label="仓库名" prop="name">
            <el-col :span="20">
              <el-input v-model="form.name" />
            </el-col>
          </el-form-item>
          <el-form-item label="备注" prop="remark">
            <el-col :span="20">
              <el-input type="textarea" v-model="form.remark" />
            </el-col>
          </el-form-item>
        </el-form>
        <template #footer>
          <div class="dialog-footer">
            <el-button @click="dialogVisible = false">取消</el-button>
            <el-button type="primary" @click="save"> 保存 </el-button>
          </div>
        </template>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { ElMessageBox } from 'element-plus';
import { reactive } from 'vue';
export default {
  name: 'StorageMange',
  data() {
    return {
      tableData: [],
      pageSize: 10,
      pageNum: 1,
      total: 0,
      name: '',
      dialogVisible: false,
      form: {
        id: '',
        name: '',
        remark: '',
      },
      rules: {
        name: [
          { required: true, message: '请输入仓库名', trigger: 'blur' },
          { min: 1, max: 8, message: '长度在1到8个字符', trigger: 'blur' },
        ],
      },
    };
  },
  methods: {
    loadPost() {
      this.$axios
        .post(this.$httpUrl + '/storage/listPage', {
          pageSize: this.pageSize,
          pageNum: this.pageNum,
          param: {
            name: this.name,
            remark: this.remark,
          },
        })
        .then((res) => res.data)
        .then((res) => {
          if (res.code == 200) {
            this.tableData = res.data;
            this.total = res.total;
          } else {
            alert('获取数据失败');
          }
        });
    },
    handleSizeChange(val) {
      this.pageNum = 1;
      this.pageSize = val;
      this.loadPost();
    },
    handleCurrentChange(val) {
      this.pageNum = val;
      this.loadPost();
    },
    resetParam() {
      this.name = '';
      this.loadPost();
    },
    resetForm() {
      this.$refs.form.resetFields();
    },
    add() {
      this.dialogVisible = true;
      this.$nextTick(() => {
        this.resetForm();
      });
    },
    del(id) {
      this.$axios
        .get(this.$httpUrl + '/storage/del?id=' + id)
        .then((res) => res.data)
        .then((res) => {
          if (res.code == 200) {
            this.$message({
              message: '删除成功！',
              type: 'success',
            });
          } else {
            this.$message.error('删除失败！');
          }
          this.loadPost();
        });
    },
    mod(row) {
      this.dialogVisible = true;
      this.$nextTick(() => {
        this.form.id = row.id;
        this.form.name = row.name;
        this.form.remark = row.remark;
      });
    },
    doSave() {
      this.$axios
        .post(this.$httpUrl + '/storage/save', this.form)
        .then((res) => res.data)
        .then((res) => {
          if (res.code == 200) {
            this.$message({
              message: '添加数据成功！',
              type: 'success',
            });
            this.dialogVisible = false;
            this.loadPost();
          } else {
            this.$message.error('添加数据失败！');
          }
        });
    },
    doMod() {
      this.$axios
        .post(this.$httpUrl + '/storage/update', this.form)
        .then((res) => res.data)
        .then((res) => {
          if (res.code == 200) {
            this.$message({
              message: '修改数据成功！',
              type: 'success',
            });
            this.form = {};
            this.dialogVisible = false;
            this.loadPost();
          } else {
            this.$message.error('修改数据失败！');
          }
        });
    },
    save() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          if (valid) {
            if (this.form.id) {
              this.doMod();
            } else {
              this.doSave();
            }
          }
        } else {
          this.$message({
            message: '操作失败!',
            type: 'error',
          });
        }
      });
    },
    handleClose() {
      ElMessageBox.confirm('你确定要关闭吗?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          this.dialogVisible = false;
        })
        .catch(() => {});
    },
  },
  created() {
    // this.loadGet();
    this.loadPost();
  },
};
</script>

<style scoped></style>
